﻿using ConsoleApplication2.business;
using ConsoleApplication2.Entity;
using ConsoleApplication2.pushApi;
using PWMIS.DataMap.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2.Dao
{ 
    /// <summary>
///  每天9点开始扫描
/// </summary>
    public class ExamArrangmentCmd
    {
        /// <summary>
        ///  使用此类必须初始化此方法
        /// </summary>                             
        public static void init()
        {
            int year = DateTime.Now.Year;

            T_ExamArrangment ment = new T_ExamArrangment();
            ment.Year = year;
            OQL oql = OQL.From(ment).Select().Where(ment.Year).END;
            AppConfig.examArrangment = MyDataHelp.FillObject<T_ExamArrangment>(oql);


        }

        /// <summary>
        /// 分3个时间段提醒的  30天  10天  3天
        /// 
        /// </summary>
        public static void selectFinish()
        {
            int[] time = new int[3] { -30, -10, -3 };
            //没学习
            foreach (int i in time)
            {
                selectStudyFinish(i, 0, "");
            }

            String wheresql = " and IsExamPassed=0";
            //没考试
            foreach (int i in time)
            {
                selectStudyFinish(i, 1, wheresql);
            }
        }


        public static void selectStudyFinish(int day, int IsStudyFinished, String whereSql)
        {

            T_ExamArrangment ment = AppConfig.examArrangment;
            DateTime dt = DateTime.Now.AddDays(day);
            if (dt.Year == ment.EndTime.Year && dt.Day == ment.EndTime.Day && dt.Year == ment.StartTime.Year && dt.Day > ment.StartTime.Day)
            {
                int number = Math.Abs(day);
                String sql = String.Format("select UserID from T_MyStudyProgress left join T_ExamArrangment on T_MyStudyProgress.ExamArrangmentid=T_ExamArrangment.Id where IsStudyFinished ={0}  and T_ExamArrangment.[Year] = {1} {2} ", IsStudyFinished, DateTime.Now.Year, whereSql);
                sendMessage(number, sql,IsStudyFinished);
            }

        }

        private static void sendMessage(int day, String sql,int IsStudyFinished)
        {
            DataTable dt = MyDataHelp.GetDataTableBySQL(sql);
            String userIDs= DataRowFactory.getRataRowToString(dt);
            if (userIDs.Length > 0)
            {
                MessagePara para = new MessagePara();
                if (0 == IsStudyFinished)
                {
                    para.Title = String.Format("本年度继续教育还有{0}天结束，请抓紧时间学习",day);
                }
                else
                {
                    para.Title = String.Format("本年度继续教育还有{0}天结束，您还没有参加考试，请抓紧时间参加考试", day);
                }
                para.UserIds = userIDs;

                HttpClientHelp.sendMessage(HttpAddress.personAddress, para);
            }

        }
    }
}
